package com.wthink.hzgj.copyFile.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.copyFile.callback.DirectoryIsEmpty
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.data.domain.FileDetail
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CopyFileCameraViewModel : ViewModel(){

    val fileDetailList = MutableLiveData(emptyList<FileDetail>())

    var list:List<FileDetail> = emptyList()

    lateinit var directoryIsEmpty: DirectoryIsEmpty

    fun setdirectoryIsEmpty(callBack: DirectoryIsEmpty) {
        directoryIsEmpty = callBack
    }

    fun query() {
        viewModelScope.launch(Dispatchers.IO) {
            val queryCameraFile = DevService.queryCameraFile()
            if (queryCameraFile is Result.Success) {
                val fileList = queryCameraFile.data.fileList
                fileDetailList.postValue(fileList)
                withContext(Dispatchers.Main) {
                    if (fileList.size<=0) {
                        toast(BusApplication.busContext, "空目录")
                    }
                }

            }
        }
    }

    fun queryDetailCheck(path:String):List<FileDetail> {
        viewModelScope.launch(Dispatchers.IO) {
            val queryCameraFile = DevService.queryCameraVideo(path)
            if (queryCameraFile is Result.Success) {
                val fileList = queryCameraFile.data.fileList
                directoryIsEmpty.fileSize(fileList.size)
                //fileDetailList.postValue(fileList)
                list = fileList
                withContext(Dispatchers.Main) {
                    if (fileList.size<=0) {
                        toast(BusApplication.busContext, "请选择文件夹")
                    }
                }
            }
        }
        return list
    }
}